package com.aaa.rabbitmq.six;

import com.aaa.rabbitmq.one.util.RabbitMQUtil;
import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.DeliverCallback;

import java.nio.charset.StandardCharsets;

/**
 * 直接模式 也叫路由模式
 */
public class ReceiveLogsDirect01 {


    //交换机的名称
    public static final String EXCHANGE_NAME = "direct_logs";

    public static void main(String[] args) throws Exception {
        Channel channel = RabbitMQUtil.getChannel();
        //声明一个交换机
        channel.exchangeDeclare(EXCHANGE_NAME, BuiltinExchangeType.DIRECT);
        //声明一个队列
        channel.queueDeclare("console",false,false,false,null);
        //交换机和队列绑定 1,队列名称 2，交换机名称 3，routingKey 路由
        channel.queueBind("console",EXCHANGE_NAME,"info");
        channel.queueBind("console",EXCHANGE_NAME,"warning");
        System.out.println("ReceiveLogsDirect01：等待接受消息，把接收到的消息打印");
        //接受消息的回调
        DeliverCallback callback = (tag, message )->{
            System.out.println("ReceiveLogsDirect01：打印收到的消息"+new String(message.getBody(), StandardCharsets.UTF_8));
        };
        //消费消息
        channel.basicConsume("console",true,callback,consumerTag ->{});


    }
}
